<?php

namespace App\Admin\Extensions\Form\Config;


use App\Models\SystemSets;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;

class ConfigBase extends Form
{
    /**
     * The form title.
     *
     * @var string
     */
    public $title = '';
    public $incType = '';
    public $switchVal = [1 => '开启', 0 => '关闭'];//开关选项

    /**
     * Handle the form request.
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request)
    {
        app('App\Http\Services\ConfigService')->saveConfig($this->incType, $request);

        admin_success('保存成功');

        return back();
    }

    /**
     * The data of the form.
     *
     * @return array $data
     */
    public function data()
    {
        $configObj  = SystemSets::where('type', $this->incType)->get();
        $aReturn = [];
        foreach ($configObj as $oneConfig) {
            if(method_exists($this, 'fieldDisplay')) {
                $this->fieldDisplay($oneConfig);
            }
            if($oneConfig->name=='index_bg'){
                $aReturn[$oneConfig->name]=json_decode($oneConfig->val,true);
            }else{
                $aReturn[$oneConfig->name] = $oneConfig->val;
            }

        }

        return $aReturn;
    }
}
